package com.igeek.boot.boot.controller;

import com.alibaba.fastjson.JSONObject;
import com.igeek.boot.boot.common.Result;
import com.igeek.boot.boot.service.LocationService;
import com.igeek.boot.boot.utils.LocationUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @package com.igeek.boot.boot.controller
 * @Description
 * @Author LittlePunk [296526219@qq.ccom]
 * @Version 2022.1
 * @Date 2023/12/19 16:03
 */
/**
 * 高德经纬度
 */
@RestController
@RequestMapping("/location")
@Slf4j
@Api(tags = "地理编码接口")
public class LocationController {

    @Autowired
    private LocationUtils locationUtils;

    @Autowired
    private LocationService service;

    /**
     * 地理编码
     * http://localhost:8080/location/get?address=?&city=?
     * @param address
     * @param city
     * @return
     */
    @GetMapping("/get")
    @ApiOperation("获取地理编码,并存入redis中")
    public Result getLocation(@RequestParam String address, @RequestParam String city){

        Map<String, String> params = new HashMap<>();
        params.put("address", address);
        params.put("city", city);
        log.info("获取地理编码,请求的参数为:{}", params);
        JSONObject map = locationUtils.getLocation(params);
        //获取 其中的坐标信息，并将坐标信息存入redis中  location: 经度,纬度
        String location = null;
        String formatted_address = null;
        try {
            location = map.getString("location");
            formatted_address = map.getString("formatted_address");
            if(location!= null &&!"".equals(location) && formatted_address!= null &&!"".equals(formatted_address)){
                String[] split = location.split(",");
                String longitude = split[0];
                String latitude = split[1];
                boolean flag = service.saveLocation(longitude, latitude,formatted_address);
                if(flag){
                    return new Result(true, "获取地理成功", map);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        log.info("获取地理编码,返回的请求结果为location:{}", location);
        return new Result(false, "获取地理失败", null);
    }

    /**
     * 逆地理编码
     * http://localhost:8080/location/getCounter?location=113.300038,40.063278
     * @param
     * @return
     */
    @GetMapping("/getCounter")
    @ApiOperation("获取逆地理编码")
    public Result getCounterLocation(@RequestParam String cityCode){
        //根据城市编码进行查询redis中是否存在该信息
        String location = service.getLocation(cityCode);
        if(location == null || "".equals(location)){
            return new Result(false, "获取地理位置失败", null);
        }
        Map<String, String> params = new HashMap<>();
        params.put("location", location);
        log.info("获取逆地理编码,请求的参数为:{}", params);
        JSONObject map = locationUtils.getCounterLocation(params);
        log.info("获取逆地理编码,返回的请求结果为:{}", map);
        return new Result(true, "获取地理位置成功", map);
    }

    //删除位置坐标信息
    @GetMapping("/del")
    public Result delLocation(@RequestParam(value="warehouseId") String id){
        boolean flag = service.deleteLocation(id);
        if(flag){
            return new Result(true, "删除成功", null);
        }
        return new Result(false, "删除失败", null);
    }

    //显示仓库里的所有坐标
    @GetMapping("/show")
    public Result show(){
        List<String> locationMembers = service.getLocationMembers();
        //使用stream获取逆地理编码并使用collect收集
        List<Map> citys = locationMembers.stream().map(cityCode -> {
            String location = service.getLocation(cityCode);
            if(location == null || "".equals(location)){
                return null;
            }
            Map<String, String> params = new HashMap<>();
            params.put("location", location);
            JSONObject map = locationUtils.getCounterLocation(params);
            return map;
        }).collect(Collectors.toList());
        return new Result(true, "获取地理位置成功", citys);
    }
}

